package entity.lemming;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import entity.Entity;
import entity.terrain.Terrain;
import level.Level;

/**
 * A Lemming that climbs vertical surfaces.
 * 
 * @author Neil Cowzer, Olga Murdoch
 */
public class Climber extends Lemming {
	
	private boolean firsttimer = true;

	/**
	 * Create an instance of the Climber Lemming.
	 * 
	 * @param x the starting x-coordinate.
	 * @param y the starting y-coordinate.
	 */
	/*@ requires 0 <= x && 0 <= y;
		assignable speed_X && speed_Y && energy;
		ensures speed_X == 0 && speed_Y < 0 && energy == 100;
	 */
	public Climber(int x, int y){
		super("resource/graphics/lemming/climber/climber_1.gif", 15,x, y);
		speed_X = 1;
		speed_Y = -1;
		energy = 100;
	}
	
	/**
	 * Create a new Climber with the given center point
	 * 
	 * @param center point of the Lemming
	 */
	public Climber(Point center){
		super("resource/graphics/lemming/climber/climber_1.gif", 15,center);
		speed_X = 1;
		speed_Y = -1;
		energy = 100;
	}
	
	/**
	 * Checks for a collision with another Entity
	 * 
	 * @param other another Entity eg Terrain, Lemming.
	 * @return true if a collision has occured.
	 */
	public /*@ pure @*/ boolean collision(Lemming other) {
/*		Rectangle me = getBounds();
		if (other instanceof Terrain){
			if (me.intersects(other.getBounds())){
				//return true;
			}
		}
*/
		return false;
	}

	/** Request that this lemming perform the appropiate action/movement. */
	/*@ ensures x == \old(x) &&
	 * 			y = \old(y) + speed_Y &&
				energy == \old(energy) - 1;
	 */
	public void move(Terrain terrain) {
		boolean adjustX = false;
		Rectangle lemming = getBounds();
		
		if (lemming.intersects(terrain.getBounds())){
			BufferedImage image = ((Terrain)terrain).getImage().getBufferedImage();
				int black = -16777216; // black that represents the background
				
				try{
					if (speed_X > 0){
						int color = 0;

						if (firsttimer){
							// Adjust x!

							if(!adjustX){
								color = image.getRGB(x + lemming.width+1, y);

								// if we have a pixel to stand on
								if (color != black){
									x+= 1;
									adjustX = true;
									System.out.println("adjusted1");
								}
							}

							if(!adjustX){
								color = image.getRGB(x + lemming.width+2, y);

								// if we have a pixel to stand on
								if (color != black){
									x+= 2;
									adjustX = true;
									System.out.println("adjusted2");
								}
							}

							if(!adjustX){
								color = image.getRGB(x + lemming.width+3, y);

								// if we have a pixel to stand on
								if (color != black){
									adjustX = true;
									System.out.println("adjusted3");
									x += 3;
								}
							}

							if(!adjustX){
								color = image.getRGB(x + lemming.width+4, y);

								// if we have a pixel to stand on
								if (color != black){
									adjustX = true;
									System.out.println("adjusted4");
									x += 4;
								}
							}
						}


						for (int curr_y = lemming.y + lemming.height; curr_y > lemming.y; curr_y--){

							color = image.getRGB(x + lemming.width, curr_y);

							// if we have a pixel to stand on
							if (color != black){
								break;
							}
							else{
								// Reached the top!
								x += 1;
								energy = 1;
							}	
						}

						if (!firsttimer) {
							for (int i = x; i < x + lemming.width; i++){
								color = image.getRGB(i, y);
								if (color != black){
									energy = 1;
									break;
								}
							}
						}
					} else{
						int color = 0;

						if (firsttimer){
							// Adjust x!

							if(!adjustX){
								color = image.getRGB(x -1, y);

								// if we have a pixel to stand on
								if (color != black){
									x-= 1;
									adjustX = true;
									System.out.println("adjusted1");
								}
							}

							if(!adjustX){
								color = image.getRGB(x -2, y);

								// if we have a pixel to stand on
								if (color != black){
									x-= 2;
									adjustX = true;
									System.out.println("adjusted2");
								}
							}

							if(!adjustX){
								color = image.getRGB(x-3, y);

								// if we have a pixel to stand on
								if (color != black){
									adjustX = true;
									System.out.println("adjusted3");
									x -= 3;
								}
							}

							if(!adjustX){
								color = image.getRGB(x -4, y);

								// if we have a pixel to stand on
								if (color != black){
									adjustX = true;
									System.out.println("adjusted4");
									x -= 4;
								}
							}
						}


						for (int curr_y = lemming.y; curr_y < lemming.y + lemming.height; curr_y++){

							color = image.getRGB(x, curr_y);

							// if we have a pixel to stand on
							if (color != black){
								break;
							}
							else{
								// Reached the top!
								x -= 1;
								energy = 1;
							}	
						}

						if (!firsttimer) {
							for (int i = x + lemming.width; i > x; i--){
								color = image.getRGB(i, y-1);
								if (color != black){
									energy = 1;
									x++;
									speed_X = 1;
									break;
								}
							}
						}

					}
				}catch(ArrayIndexOutOfBoundsException e){
					
				}
		}

		firsttimer = false;
		//update positions
		y= y+speed_Y;
		update();
		energy--;

	}
	
	/** Reverse a Lemmings direction */
	public void turnAround(){
		speed_X = -(speed_X);
		if (speed_X < 0){
			generateFrames("resource/graphics/lemming/climber/climberLeft_1.gif", 15);
		}else{
			generateFrames("resource/graphics/lemming/climber/climber_1.gif", 15);
		}
	}

}
